package logic

import (
	"api/model/mysql"
	"context"
	"errors"
	"house_srv/house"
	"house_srv/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddHouseLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAddHouseLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddHouseLogic {
	return &AddHouseLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *AddHouseLogic) AddHouse(in *house.AddHouseRequest) (*house.AddHouseResponse, error) {

	var u mysql.User
	mysql.DB.Find(&u)

	if u.UserStatus != 1 {
		logx.Error("该用户没有发布权限")
		return nil, errors.New("该用户没有发布权限")
	}

	m := mysql.House{
		HouseCity:    in.HouseCity,
		HouseAddress: in.HouseAddress,
		HouseBuild:   in.HouseBuild,
		HouseFloor:   in.HouseFloor,
		HouseArea:    int(in.HouseArea),
		HousePrice:   float64(in.HousePrice),
		HouseAuthor:  in.HouseAuthor,
		HouseStatus:  1,
	}

	mysql.DB.Create(&m)

	return &house.AddHouseResponse{
		Id: int64(m.ID),
	}, nil
}
